//
//  EmailResetPasswordViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class EmailResetPasswordViewController: AccountBaseViewController,InputViewDelegate {

    @IBOutlet weak var emailInput: InputView!
    @IBOutlet weak var imageCodeInput: InputView!
    @IBOutlet weak var nextBtn: SureButton!
    
    lazy var registerViewModel:RegisterOrResetViewModel = RegisterOrResetViewModel(codeType: 3)

    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    func initUI() {
        self.title = kText("邮箱找回密码")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: kText("手机号"), style: .plain, target: self, action: #selector(phoneReset))
        self.emailInput.delegate = self
        self.emailInput.placeholder = kText("请输入您的邮箱")
        self.imageCodeInput.delegate = self
        self.imageCodeInput.style = .imageVerify
        self.imageCodeInput.placeholder = kText("请输入图片验证码")
        self.nextBtn.setTitle(kText("下一步"), for: .normal)
        self.nextBtn.isEnabled = false
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        initData()
    }
    
    func initData() {
        registerViewModel.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_imageVerify {
                if isSuccess {
                    self?.imageCodeInput.imageCode = self?.registerViewModel.image
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }else if state == ResponseState.mine_emailCode {
                if isSuccess {
                    let vc = SecondRegisterOrResetViewController()
                    vc.type = 3
                    vc.viewModel = self?.registerViewModel
                    self?.navigationController?.pushViewController(vc, animated: true)
                }else {
                    if let msg = errorMsg {
                         CYMBShow.showMessage(msg)
                    }
                }
            }
        }
        registerViewModel.getImageCode()
    }
    
    // MARK: - event
    @objc func phoneReset() {
        dPrint("切换手机号找回")
        let vc =  MobileResetPasswordViewController()
        self.navigationController?.customReplaceTopPushViewController(vc, animated: true)
    }
    
    @IBAction func next(_ sender: SureButton) {
        dPrint("next")
        self.view.endEditing(true)
        let email = self.emailInput.input.text ?? ""
        self.registerViewModel.sendEmailCode(email: email, imageCode: self.imageCodeInput.input.text!)
    }
    
    func inputViewTextDidChange(inputView: InputView) {
        dPrint("inputViewTextDidChange")
        if let emailCount = self.emailInput.input.text?.count,let imageCodeCount = self.imageCodeInput.input.text?.count, emailCount > 0,  imageCodeCount > 0 {
            self.nextBtn.isEnabled = true
        }else {
            self.nextBtn.isEnabled = false
        }
    }
    func inputViewImageVerifyClick(inputView: InputView) {
        dPrint("inputViewImageVerifyClick")
        self.registerViewModel.getImageCode()
    }
}
